package com.thread.method_runtime;

import java.io.OutputStream;

public class RuntimeSender {

	private OutputStream os;
	Runtime r;

	/**
	 * 执行shell指令
	 * 
	 * @param cmd
	 *            指令
	 */
	public final void exec(String cmd) {
		try {
			if (os == null) {
				os = Runtime.getRuntime().exec("su").getOutputStream();//需要root权限，不然怎么点悬浮窗都没用
			}
			os.write(cmd.getBytes());
			os.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public final void exec2(String cmd) {
		try {
			if (r == null) {
				r = Runtime.getRuntime();
			}
			r.exec(cmd);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 后台模拟全局按键
	 * 
	 * @param keyCode
	 *            键值
	 */
	public final void simulateKey(int keyCode) {
		exec("input keyevent " + keyCode + "\n");
	}
}